package worldinruin.core.imp.tilemodels;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.ArrayList;

import worldinruin.core.IBuilding;
import worldinruin.core.ITile;
import worldinruin.core.ITroop;
import worldinruin.resources.Events;

public abstract class AbstractTile implements ITile, Serializable {
	
	private static final long serialVersionUID = 1533834415223491283L;
	private IBuilding building;
	private PropertyChangeSupport propertyChangeSupport;
	private ArrayList<ITroop> troops = new ArrayList<ITroop>();
	
	{
		propertyChangeSupport = new PropertyChangeSupport(this);
	}
	
	

	@Override
	public void addTroop(ITroop t) {
		ITroop oldValue = t;
		troops.add(t);
		
		propertyChangeSupport.firePropertyChange(Events.troopAdded, oldValue, getTroops());
	}
	
	@Override
	public void removeTroop(ITroop t) {
		ITroop oldValue = t;
		troops.remove(t);		
		
		propertyChangeSupport.firePropertyChange(Events.troopRemoved, oldValue, getTroops());
	}

	
	@Override
	public ArrayList<ITroop> getTroops() {
		return troops;
	}

	@Override
	public void addBuilding(IBuilding b) {
		IBuilding oldValue = building;
		building = b;
		
		propertyChangeSupport.firePropertyChange(Events.buildingAdded, oldValue, this.getBuilding());
	}

	@Override
	public void removeBuilding(IBuilding b) {
		IBuilding oldValue = building;
		building = null;
		
		propertyChangeSupport.firePropertyChange(Events.buildingRemoved, oldValue, null);
	}


	@Override
	public IBuilding getBuilding() {
		return building;
	}
	
	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl){
		propertyChangeSupport.addPropertyChangeListener(pcl);
	}
	
	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl){
		propertyChangeSupport.removePropertyChangeListener(pcl);
	}
}
